var slider_scroll_timeout = 1000; // 3500
var slider_current_index = 1;
var slider_user_interacted = false;
var slider_loaded_instance;

function mycarousel_initCallback(carousel) {

    slider_loaded_instance = carousel;


    jQuery('.dot').bind('click', function() {

        if(slider_loaded_instance.animating) return false;

        carousel.options.auto = 0;


		var dots = $('.dot');


        var clickedIndex  = jQuery.jcarousel.intval(dots.index(this) + 1);
        var currentActualIndex = getActualIndex(slider_current_index);
        var dif = clickedIndex - currentActualIndex;
        slider_loaded_instance.scroll(slider_current_index + dif -1 );


        return false;
    });


    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('.jFlowNext').bind('click', function() {
		carousel.options.auto = 0;
        carousel.next();
        return false;
    });

    jQuery('.jFlowPrev').bind('click', function() {
		carousel.options.auto = 0;
        carousel.prev();
        return false;
    });
};


function mycarousel_newItemVisible(carousel, obj, index, state){
    slider_current_index = index;
    showgallery(getActualIndex(index));
}

function getActualIndex(ind){

    var itemsTotal = $('.dot').size();
    return ((ind-1) % (itemsTotal));
}

function showgallery (id1)
{
	$('.dot').removeClass('activedot');
	$($('.dot')[id1]).addClass('activedot');
}

jQuery(document).ready(function() {
    jQuery("#mycarousel").jcarousel({
        scroll: 1,
        auto: 2,
        wrap: 'circular',
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null,
        itemVisibleInCallback: {
            onAfterAnimation: mycarousel_newItemVisible,
			onBeforeAnimation: mycarousel_newItemVisible
        }
});



});